import { useState, useEffect } from 'react'
import $api from '../../../request/api'
import { Space, Switch, Button, Modal, Input, Form, Tag, Select } from 'antd'
import { ExclamationCircleOutlined } from '@ant-design/icons'

function useGetUser(path) {
  // 定义一个user数据
  let [users, setUsers] = useState([])
  // 获取所有用户的数据
  const getUser = (path) => {
    let arr = []
    if (path == 'normal') {
      $api.getNormal().then((res) => {
        if (res.result) {
          res.result.forEach((item) => {
            arr.push(Object.assign(item, { key: item.id }))
          })
        }
        setUsers(arr)
      })
    } else if (path == 'traffic') {
      $api.getTraffic().then((res) => {
        if (res.result) {
          res.result.forEach((item) => {
            arr.push(Object.assign(item, { key: item.id }))
          })
        }
        setUsers(arr)
      })
    }
  }
  // 根据搜索的内容改变user数据的内容
  const changeUser = (res) => {
    let arr = []
    if (res.result) {
      res.result.forEach((item) => {
        arr.push(Object.assign(item, { key: item.id }))
      })
    }
    setUsers(arr)
  }
  // 获取搜索数据
  const getSearchUser = (value) => {
    if (value) {
      $api.getSearchUser({ value: value, type: path }).then((res) => {
        changeUser(res)
      })
    } else {
      getUser(path)
    }
  }
  // 删除用户
  function deleteUser(record) {
    $api.deleteUser(record.id).then((res) => {
      if (res.code == 200) {
        Modal.success({
          content: '删除成功',
          okText: '确认',
        })
        getUser(path)
      } else {
        Modal.warning({
          title: '删除失败',
          content: '删除出错!',
        })
      }
    })
  }
  // 删除选中
  function delSelectUser(selectedRowKeys) {
    $api.postDelSelceted(selectedRowKeys).then((res) => {
      if (res.code == 200) {
        Modal.success({
          content: '删除成功',
          okText: '确认',
        })
        getUser(path)
      } else {
        Modal.warning({
          title: '删除失败',
          content: '删除出错!',
        })
      }
    })
  }
  // 修改用户名和密码
  const [form] = Form.useForm()
  function changeUserValue(record) {
    setTimeout(() => {
      form.setFieldsValue({
        username: record.username,
        password: record.password,
      })
    }, 1)
    // 弹框实现
    Modal.info({
      title: '修改用户信息',
      content: (
        <Form form={form} layout="vertical" name="form_in_modal">
          <Form.Item
            name="username"
            label="用户名"
            rules={[
              {
                required: true,
                message: '用户名不能为空!',
              },
            ]}
          >
            <Input />
          </Form.Item>
          <Form.Item
            name="password"
            label="密码"
            rules={[
              {
                required: true,
                message: '新密码不能为空!',
              },
            ]}
          >
            <Input.Password />
          </Form.Item>
        </Form>
      ),
      onOk() {
        form
          .validateFields()
          .then((values) => {
            $api
              .postChangeUser({
                id: record.id,
                username: values.username,
                password: values.password,
              })
              .then((res) => {
                if (res.code == 200) {
                  Modal.success({
                    content: '修改成功',
                    okText: '确认',
                  })
                  getUser(path)
                } else {
                  Modal.warning({
                    title: '修改失败',
                    content: '修改出错!',
                  })
                }
              })
            form.resetFields()
          })
          .catch((info) => {
            console.log('Validate Failed:', info)
          })
      },
    })
  }
  // 新增用户
  const [addForm] = Form.useForm()
  const types = [
    { label: 'normal', value: 'normal' },
    { label: 'traffic', value: 'traffic' },
  ]
  function addUser() {
    // 弹框实现
    Modal.info({
      title: '新增用户',
      content: (
        <Form form={addForm} layout="vertical" name="form_in_modal_add">
          <Form.Item
            name="username"
            label="用户名"
            rules={[
              {
                required: true,
                message: '用户名不能为空!',
              },
            ]}
          >
            <Input />
          </Form.Item>
          <Form.Item
            name="password"
            label="密码"
            rules={[
              {
                required: true,
                message: '密码不能为空!',
              },
            ]}
          >
            <Input.Password />
          </Form.Item>
          <Form.Item
            name="type"
            label="类型"
            rules={[{ required: true, message: '类型不能为空' }]}
          >
            <Select options={types} />
          </Form.Item>
        </Form>
      ),
      onOk() {
        addForm
          .validateFields()
          .then((values) => {
            $api.postAddUser(values).then((res) => {
              if (res.code == 200) {
                Modal.success({
                  content: '新增成功',
                  okText: '确认',
                })
                getUser(path)
              } else {
                Modal.warning({
                  title: '新增失败',
                  content: '新增出错!',
                })
              }
            })
            addForm.resetFields()
          })
          .catch((info) => {
            console.log('Validate Failed:', info)
          })
      },
    })
  }
  useEffect(() => {
    getUser(path)
  }, [])
  // 返回值
  return {
    users,
    setUsers,
    getUser,
    getSearchUser,
    deleteUser,
    delSelectUser,
    changeUserValue,
    addUser,
  }
}

export { useGetUser }
